Page({
  data: {
    dialogVisible: false,
    houseList: {},
    isEmpty: false,
    cellId: ''
  },
  onShow () {
    this.getHouseList()
  },
  async getHouseList () {
    const { data } = await wx.http.get('/room')
    console.log(data)
    this.setData({ houseList: data, isEmpty: data.length === 0 })
  },
  swipeClose (ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
        cellId: ev.mark.id,
        cellIndex: ev.mark.index
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  dialogClose (ev) {
    console.log(ev)
    if (ev.detail === 'confirm') this.deleteHouse()
  },
  async deleteHouse () {
    await wx.http.delete('/room/' + this.data.cellId)
    this.data.houseList.splice(this.cellIndex, 1)
    this.setData({
      houseList: this.data.houseList,
      isEmpty: this.data.houseList.length === 0
    })
  },
  goDetail (ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id
    })
  },

  addHouse () {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index'
    })
  }
})
